home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / smallc.arc / SQRT.C < prev    next >
Text File  |  1985-07-17  |  1KB  |  39 lines

  1.  
  2. /***********************************************************
  3.  *               The TULSA IBM C BOARD                     *
  4.  *                   918-664-8737                          *
  5.  *             300/1200 XMODEM, 24 Hours                   *
  6.  **********************************************************/
  7.  
  8.  
  9. #include "math.h"
  10. #include "errno.h"
  11.  
  12. double sqrt(x)
  13. double x;
  14. {
  15.         double f, y;
  16.         int n;
  17.         extern int errno;
  18.  
  19.         if (x == 0.0)
  20.                 return x;
  21.         if (x < 0.0) {
  22.                 errno = EDOM;
  23.                 return 0.0;
  24.         }
  25.         f = frexp(x, &n);
  26.         y = 0.41731 + 0.59016 * f;
  27.         y = (y + f/y);
  28.         y = ldexp(y,-2) + f/y;  /* fast calculation of y2 */
  29.         y = ldexp(y + f/y, -1);
  30.         y = ldexp(y + f/y, -1);
  31.  
  32.         if (n&1) {
  33.                 y *= 0.70710678118654752440;
  34.                 ++n;
  35.         }
  36.         return ldexp(y,n/2);
  37. }
  38.  
  39.